package com.wuhuiyue.controller;


import com.alibaba.excel.EasyExcel;
import com.wuhuiyue.entity.Center;
import com.wuhuiyue.entity.Result;
import com.wuhuiyue.service.ICenterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author hy-woo
 * @since 2022-06-14
 */
@RestController
@RequestMapping("/center")
public class CenterController {

    @Autowired
    ICenterService centerService;

    @GetMapping("excel")
    public void download(HttpServletResponse response) throws IOException {
        // 这里注意 有同学反应使用swagger 会导致各种问题，请直接用浏览器或者用postman
        response.setContentType("application/vnd.openxmlformats-officedocument.spreadsheetml.sheet");
        response.setCharacterEncoding("utf-8");
        // 这里URLEncoder.encode可以防止中文乱码 当然和easyexcel没有关系
        String fileName = URLEncoder.encode("渠道类型统计", "UTF-8").replaceAll("\\+", "%20");
        response.setHeader("Content-disposition", "attachment;filename*=utf-8''" + fileName + ".xlsx");
        List<Center> list=centerService.getCenter();
        EasyExcel.write(response.getOutputStream(), Center.class).sheet("渠道类型统计").doWrite(list);
    }

    @RequestMapping("/list")
    public Result list(int type){
        List<Center> list=centerService.getByType(type);
        return Result.OK(list);
    }

}
